package com.party.controller.sys.vote;

import com.party.common.Page;
import com.party.common.Tools;
import com.party.model.Vote;
import com.party.model.SysUser;
import com.party.service.sys.SysUserService;
import com.party.service.vote.VoteService;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping(value = "/sys/vote")
public class SysVoteController {

    @Resource
    VoteService voteService;

    @Resource
    SysUserService sysUserService;

    private int pageSize = 9;

    /**
     * 首页
     * @return
     */
    @RequestMapping(value = "/index")
    public ModelAndView index() {
        ModelAndView modelAndView = new ModelAndView();
        modelAndView.setViewName("/sys/vote/index");
        return modelAndView;
    }

    /**
     * 获取所有人
     * @return
     */
    @RequestMapping(value = "/allPersons")
    public ModelAndView excellentParty() {
        ModelAndView modelAndView = new ModelAndView();
        List<SysUser> listUser=sysUserService.getAllUsers();
        modelAndView.addObject("listUser",listUser);
        modelAndView.setViewName("/sys/vote/voteperson");
        return modelAndView;
    }

    /**
     * 查询数据
     * @param req
     * @return
     */
    @RequestMapping(value = "/query")
    @ResponseBody
    public Map<String,Object> query(HttpServletRequest req) {
        Map<String, Object> map = new HashMap<String, Object>();
        String pageIndexStr = req.getParameter("pageIndex");
        String title = req.getParameter("title");
        String creatTime = req.getParameter("creatTime");

        int pageIndex = 1;
        if (pageIndexStr != null && !"".equals(pageIndexStr)) {
            pageIndex = Integer.parseInt(pageIndexStr);
        }

        Vote vote = new Vote();
        vote.setTitle(title);
        vote.setCreattime(creatTime);
        Page p = this.voteService.getVoteList(vote, pageSize, pageIndex);

        map.put("pageCount",p.getTotalPageCount() );
        map.put("tableData",p.getResult());
        return map;
    }

    /**
     * 增加
     * @return
     */
    @RequestMapping(value = "/add")
    public ModelAndView add() {
        ModelAndView modelAndView = new ModelAndView();
        modelAndView.setViewName("/sys/vote/add");
        return modelAndView;
    }

    /**
     * 增加数据
     * @return
     */
    @RequestMapping(value = "/addData")
    @ResponseBody
    public Map<String,Object> addData(
            @RequestParam(value = "title")String title,
            @RequestParam(value = "overtime")String overtime,
            @RequestParam(value = "count")Integer count,
            @RequestParam(value = "min")Integer min,
            @RequestParam(value = "max")Integer max,
            @RequestParam(value = "isover")String isover,

            @RequestParam(value = "content")String content,
                                      HttpSession session,
                                      HttpServletRequest request,
                                      HttpServletResponse response) {

        String msg="";
        Vote vote=new Vote();
        vote.setTitle(title);
        vote.setOvertime(overtime);
        vote.setCount(count);
        vote.setMin(min);
        vote.setMax(max);
        vote.setIsover(isover);
        vote.setContent(content.getBytes());
        if ((SysUser) session.getAttribute("sysUser") != null) {
            int uid = ((SysUser) session.getAttribute("sysUser")).getUid();
            vote.setUid(uid);
        }

        String time=Tools.getNowDate();
        vote.setCreattime(time);
        int num=voteService.addVote(vote);
        if(num>0){
            msg="添加成功";
        }else{
            msg="添加失败";
        }
        Map<String, Object> map = new HashMap<String, Object>();

        map.put("msg",msg);
        return map;
    }

    /**
     * 编辑
     * @return
     */
    @RequestMapping(value = "/edit")
    public ModelAndView edit(HttpServletRequest request) {
        ModelAndView modelAndView = new ModelAndView();
        String id=request.getParameter("id");
        String msg="";
        Vote vote=null;
        if(!"".equals(id)){
            int intId=Integer.parseInt(id);
            vote=voteService.getVoteByPrimaryKey(intId);
        }

        if(vote==null){
            msg="获取成功";
        }else{
            msg="获取失败";
        }

        byte[]binaryContent=vote.getContent();
        String content="";
        if(binaryContent!=null){
            content=new String(binaryContent);
        }

        modelAndView.addObject("content",content);

        modelAndView.addObject("vote",vote);
        modelAndView.setViewName("/sys/vote/edit");
        return modelAndView;
    }

    /**
     * 编辑
     * @return
     */
    @RequestMapping(value = "/editData")
    @ResponseBody
    public Map<String,Object> editData(
            @RequestParam(value = "id")Integer id,
            @RequestParam(value = "title")String title,
            @RequestParam(value = "overtime")String overtime,
            @RequestParam(value = "count")Integer count,
            @RequestParam(value = "min")Integer min,
            @RequestParam(value = "max")Integer max,
            @RequestParam(value = "isover")String isover,
            @RequestParam(value = "content")String content,
                                  HttpSession session,
                                  HttpServletRequest request,
                                  HttpServletResponse response) {
        String msg="";
        Vote vote=new Vote();
        vote.setId(id);
        vote.setTitle(title);
        vote.setOvertime(overtime);
        vote.setCount(count);
        vote.setMin(min);
        vote.setMax(max);
        vote.setIsover(isover);
        vote.setContent(content.getBytes());
        int num=voteService.updateVoteByPrimaryKey(vote);
        if(num>0){
            msg="编辑成功";
        }else{
            msg="编辑失败";
        }

        Map<String, Object> map = new HashMap<String, Object>();

        map.put("msg",msg);

        return map;
    }


    /**
     * 删除
     *
     * @param ids
     * @param session
     * @param request
     * @param response
     * @return
     */
    @RequestMapping(value = "/delete")
    @ResponseBody
    public Map<String, Object> delete(
            @RequestParam("ids") String ids,
            HttpSession session,
            HttpServletRequest request,
            HttpServletResponse response
    ) {

        int num = 0;
        try {
            num = voteService.deleteVoteByIds(ids);

        } catch (Exception e) {
            num = -1;
            e.printStackTrace();
        }
        Map<String, Object> modelMap = new HashMap<String, Object>();
        modelMap.put("num", num);
        return modelMap;
    }
}
